import java.util.*;
import java.util.stream.Collectors;

public class SalesReport {
    public void dailyReport(List<Show> shows) {
        Date today = new Date();
        long start = today.getTime() / (24 * 3600 * 1000) * (24 * 3600 * 1000);
        long end = start + 24 * 3600 * 1000;

        generateReport(shows, start, end, "今日");
    }

    public void weeklyReport(List<Show> shows) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 设置为本周一
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        long start = calendar.getTimeInMillis();

        // 设置为下周一（本周日结束）
        calendar.add(Calendar.DAY_OF_YEAR, 7);
        long end = calendar.getTimeInMillis();

        generateReport(shows, start, end, "本周");
    }

    public void monthlyReport(List<Show> shows) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 设置为当月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        long start = calendar.getTimeInMillis();

        // 设置为下月第一天（当月最后一天结束）
        calendar.add(Calendar.MONTH, 1);
        long end = calendar.getTimeInMillis();

        generateReport(shows, start, end, "本月");
    }

    private void generateReport(List<Show> shows, long startTime, long endTime, String period) {
        List<Show> filteredShows = shows.stream()
                .filter(s -> s.getShowTime().getTime() >= startTime && s.getShowTime().getTime() < endTime)
                .collect(Collectors.toList());

        int totalTickets = filteredShows.stream().mapToInt(s -> s.getTickets().size()).sum();
        double totalRevenue = filteredShows.stream().mapToDouble(s -> s.getTickets().size() * s.getPrice()).sum();

        // 实际应用中应从日志或专门的退票记录统计
        int refundCount = 0;

        // 按影片统计销售情况
        Map<String, Integer> movieSales = new LinkedHashMap<>();
        Map<String, Double> movieRevenues = new LinkedHashMap<>();

        filteredShows.forEach(show -> {
            String movieName = show.getMovie().getName();
            int tickets = show.getTickets().size();
            double revenue = tickets * show.getPrice();

            movieSales.put(movieName, movieSales.getOrDefault(movieName, 0) + tickets);
            movieRevenues.put(movieName, movieRevenues.getOrDefault(movieName, 0.0) + revenue);
        });

        System.out.println(period + "销售报表:");
        System.out.println("总售票数: " + totalTickets);
        System.out.println("总销售额: " + totalRevenue);
        System.out.println("退票数: " + refundCount);
        System.out.println("\n影片销售详情:");

        movieSales.forEach((movie, tickets) -> {
            double revenue = movieRevenues.get(movie);
            System.out.printf("%-20s 售票数: %-5d 销售额: %.2f%n",
                    movie, tickets, revenue);
        });
    }
}